define(function() {
	var ViewModel = function() {
		var self = this;
		self.galleryUploader = new qq.FineUploader({
	        debug: true,
	        element: document.getElementById('fine-uploader'),
	        request: { endpoint: '/FileUpload/UploadFile', method: 'POST' },
	        validation: {
	            allowedExtensions: ['xls', 'xlsx']
	        },
	        callbacks: {
	            onAllComplete: function (successIDs, failIDs) {
	                console.log(successIDs);
	            },
	            onComplete: function (id, fileName, responseJSON) {
	                layer.alert('文件上传完成', {
	                    icon: 16,
	                    shade: 0.6
	                });
	               var  uploadId = responseJSON.uploadId;
	                $.cookie('uploadId', responseJSON.uploadId);
	                $.ajax({
                        url: '/goods/process/' + uploadId,
                        method: 'POST',
                        success: function(data) {
                        	layer.closeAll();
                        	if(data.status==1){
                        		toastr.error(data.msg);
                        	}
                            layer.msg('解析完成', {
                                icon: 16,
                                shade: 0.6,
                                time: 1500
                            });
                        },
                        error: function(data) {
                            layer.msg('解析失败，请联系管理员...'+data.msg, {
                                icon: 16,
                                shade: 0.6
                            });
                        }
                    })
	            },
	            onUpload: function () {
	                layer.alert('文件上传中', {
	                    icon: 16,
	                    shade: 0.6
	                });
	            }
	        }
	    });

		// 这里是分页配置,第一个参数是查询的地址，第二个参数是分页大小，第三个参数是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax('/goods/findAllGoods',
				20, self.customer_process);
		self.customer_process = function(data) {
			if (data && data.length && data.length > 0) {
				$.each(data, function(index, val) {
					val.checked = false;
				});
			}
			var items = ko.mapping.fromJS(data);
			return items();
		};

		self.filter = ko.observable({
			goodsname : '',
			goodscode : '',
			hscode:''
		});
		// 清空
		self.empty = function() {
			self.filter({
				goodsname : '',
				goodscode : '',
				hscode:''
			});
		}
		// 查询
		self.search = function() {
			var data = self.filter();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		};
		 
		self.model=ko.observable({
			goodsname : '',
			goodscode : '',
			hscode:'',
			unit:'',
			unit1:'',
			unit2:'',
			goodsdescription:''
		})
		// 修改
		self.update = function(data) {
			layer.open({
				type : 1,
				title : "修改商品信息",
				area : [ '400px', '300px' ],
				content : $('#pop_div_goods')
			});
			self.model(data);
		};
		// 关闭客户窗口信息
		self.layerClose = function() {
			layer.close(layer.index);
			toastr.info("已关闭");
		};
		// 保存客户信息
		self.save = function() {
			var data = self.model();
			$.ajax({
				url : "goods/updateGoods",
				type : 'post',
				contentType : 'application/json',
				data : JSON.stringify(data),
				dataType : 'json',
				cache : false,
				success : function(result) {
					if (result.status == 0) {
						toastr.success(result.data, result.msg);
						layer.close(layer.index);
						self.search();
					} else {
						toastr.error(result.msg);
					}
				}
			})
		}
		
		self.submit=function(){
			var data=self.model();
		}
		
		self.deleteGoods=function(goods){
			console.log(goods.goodsid)
			var data=goods.goodsid
			// 询问框
			layer.confirm('你确定要删除吗？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				$.ajax({
					url : "goods/deleteGoods",
					type : 'post',
					contentType : 'application/json',
					data : JSON.stringify(goods),
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
							layer.close(layer.index);
							self.search();
						} else {
							toastr.error(result.msg);
						}
					}
				})
				layer.msg('确定删除', {
					icon : 1
				});
			}, function() {
				layer.msg('已经取消', {
					time : 1000, 
				});
			});
		}
		
		self.search();
	}
	var model = new ViewModel();
	ko.applyBindings(model);
});